Use case: HAL CORDIC calculate in interrupt mode

Sequence diagram

CORDIC calculation in interrupt mode

  @startuml

  participant "User application\n(thread mode)" as appli
  participant "<b>HAL CORDIC</b>" as cordic
  participant "CORDIC IRQ handler" as isr

  == Initialization / configuration ==
  appli -> cordic : HAL_CORDIC_Init(&hcordic)
  appli -> cordic : HAL_CORDIC_Configure(&hcordic, &cordic_config)
  appli -> appli : HAL_NVIC_SetPriority(CORDIC_IRQn, ...)
  appli -> appli : HAL_NVIC_EnableIRQ(CORDIC_IRQn)

  == Calculation ==
  appli -> cordic : HAL_CORDIC_Calculate_IT(&hcordic, input_data, output_data, n)
  cordic --> appli : HAL status

  == IRQ / callback ==
  isr -> cordic : CORDIC_IRQHandler()\nHAL_CORDIC_IRQHandler(&hcordic)
  cordic -> appli : HAL_CORDIC_CalculateCpltCallback(&hcordic)

  @enduml

Code snippet

HAL1

HAL2

#define INPUT_BUFFER_SIZE 4U
#define OUTPUT_BUFFER_SIZE 4U

int32_t input_data[INPUT_BUFFER_SIZE] =  {0x80000000, 0x90000000, 0xA0000000, 0xB0000000};
int32_t output_data[OUTPUT_BUFFER_SIZE] = {0U};









CORDIC_HandleTypeDef hcordic;
CORDIC_ConfigTypeDef cordic_config;

hcordic.Instance = CORDIC;
HAL_CORDIC_Init(&hcordic);

__HAL_RCC_CORDIC_CLK_ENABLE();

cordic_config.Function         = CORDIC_FUNCTION_SINE;
cordic_config.Precision        = CORDIC_PRECISION_6CYCLES;
cordic_config.Scale            = CORDIC_SCALE_0;
cordic_config.NbWrite          = CORDIC_NBWRITE_1;
cordic_config.NbRead           = CORDIC_NBREAD_1;
cordic_config.InSize           = CORDIC_INSIZE_32BITS;
cordic_config.OutSize          = CORDIC_OUTSIZE_32BITS;

HAL_CORDIC_Configure(&hcordic, &cordic_config)

HAL_NVIC_SetPriority(CORDIC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CORDIC_IRQn);

HAL_CORDIC_Calculate_IT(&hcordic, input_data, output_data, 4);

/* Wait for the calculation complete callback:
   HAL_CORDIC_CalculateCpltCallback() */
#define INPUT_BUFFER_SIZE 4U
#define OUTPUT_BUFFER_SIZE 4U

int32_t input_data[INPUT_BUFFER_SIZE] =  {0x80000000, 0x90000000, 0xA0000000, 0xB0000000};
int32_t output_data[OUTPUT_BUFFER_SIZE] = {0U};

hal_cordic_buffer_desc_t input_buffer = {
    .p_data = input_data,
    .size_word = INPUT_BUFFER_SIZE};

hal_cordic_buffer_desc_t output_buffer = {
    .p_data = output_data,
    .size_word = OUTPUT_BUFFER_SIZE};

hal_cordic_handle_t hcordic;
hal_cordic_config_t cordic_config;


HAL_CORDIC_Init(&hcordic, HAL_CORDIC);

HAL_RCC_CORDIC_EnableClock();

cordic_config.function   = HAL_CORDIC_FUNCTION_SINE;
cordic_config.precision  = HAL_CORDIC_PRECISION_6_CYCLE;
cordic_config.scaling_factor = HAL_CORDIC_SCALING_FACTOR_0;
cordic_config.nb_args    = HAL_CORDIC_NB_ARG_1;
cordic_config.nb_results = HAL_CORDIC_NB_RESULT_1;
cordic_config.in_width    = HAL_CORDIC_IN_WIDTH_32_BIT;
cordic_config.out_width   = HAL_CORDIC_OUT_WIDTH_32_BIT;

HAL_CORDIC_SetConfig(&hcordic,&cordic_config);

HAL_CORTEX_NVIC_SetPriority(CORDIC_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(CORDIC_IRQn);

HAL_CORDIC_Calculate_IT(&hcordic, &input_buffer, &output_buffer);

/* Wait for the calculation complete callback:
   HAL_CORDIC_CalculateCpltCallback() */

Key changes

  1. HAL2 uses buffer descriptors (hal_cordic_buffer_desc_t) for inputs and outputs instead of raw pointers.

  2. HAL2 uses HAL_CORTEX_NVIC_SetPriority() and HAL_CORTEX_NVIC_EnableIRQ() for NVIC configuration.

  3. HAL2 explicitly enables the peripheral clock using HAL_RCC_CORDIC_EnableClock().